﻿Imports DTO
Imports System.Data.Linq

Public Class PhieuMuonDao
    Public Shared Function LayBang() As Table(Of PhieuMuonSach)
        Dim db As New ThuVienDataContext()
        Return db.PhieuMuonSaches
    End Function

    Public Shared Function LayDanhSach() As IEnumerable(Of PhieuMuonSach)
        Dim lst As IEnumerable(Of PhieuMuonSach)
        Dim db As New ThuVienDataContext()
        lst = From pt In db.PhieuMuonSaches Select pt
        Return lst
    End Function

    Public Shared Function LayPhieuMuon(ByVal mt As Integer) As PhieuMuonSach
        Dim db As New ThuVienDataContext()
        Dim lst = From p In db.PhieuMuonSaches Where p.MT = mt Order By p.Ngay_muon Descending Take 1
        Dim phieu As New PhieuMuonSach

        If lst.Count > 0 Then
            phieu = lst.Single
        Else
            phieu.MPM = -1
        End If

        Return phieu
    End Function

    Public Shared Sub Them(ByVal t As PhieuMuonSach)
        Dim db As New ThuVienDataContext()
        db.PhieuMuonSaches.InsertOnSubmit(t)
    End Sub

    Public Shared Sub ThemChiTiet(ByVal m As PhieuMuonSach, ByVal t As ChiTietMuonTra)
        Dim db As New ThuVienDataContext()
        db.PhieuMuonSaches.InsertOnSubmit(m)
        db.SubmitChanges()
        t.MPM = m.MPM
        db.ChiTietMuonTras.InsertOnSubmit(t)
        db.SubmitChanges()
    End Sub
End Class
